package cn.dyp.com.getsensordata;


import android.app.Activity;
import android.app.AlertDialog;
import android.app.Application;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;

import cn.dyp.com.getsensordata.R;
import cn.dyp.com.getsensordata.bean.SensorConfig;
import cn.dyp.com.getsensordata.bean.SensorValue;
import cn.dyp.com.getsensordata.request.BaseRequest;
import cn.dyp.com.getsensordata.request.RequestThread;
import cn.dyp.com.getsensordata.util.Util;

/**
 * 程序启动的入口 提供全局的数据存储
 * 
 * @author asus
 */

public class ClientApp extends Application {
	private SharedPreferences mSharedPreferences;
	private String serverIpStr = "";
	private SensorValue mCurSensorValue;
	private SensorConfig mSensorConfig;
	private Handler mHandler;
	private AlertDialog mDialog;

	@Override
	public void onCreate() {
		super.onCreate();
		mSharedPreferences = PreferenceManager
				.getDefaultSharedPreferences(this);
		mCurSensorValue = new SensorValue();
		mSensorConfig = new SensorConfig();
		readSensorConfig();
		handleMessage();
	}

	/**
	 * 接收消息进行处理
	 * 
	 */
	private void handleMessage() {
		mHandler = new Handler(new Handler.Callback() {

			@Override
			public boolean handleMessage(Message msg) {
				switch (msg.what) {
				case RequestThread.MSG_REQUEST_RESULT:
					BaseRequest.RequestResult result = BaseRequest.RequestResult.values()[msg.arg1];
					RequestThread requestThread = (RequestThread) msg.obj;
					if (requestThread != null) {
						requestThread.hanlderResult(result);
					}
					if (result.equals(BaseRequest.RequestResult.RESULT_NO_NET)) {
						if (mDialog != null) {
							if (!mDialog.isShowing()) {
								mDialog.show();
							}
						}
					}
					break;
				}
				return false;
			}
		});
	}

	/**
	 * 获取Ip地址
	 * 
	 * @return Ip地址
	 */
	public String getServerIpStr() {
		serverIpStr = readIpStr();
		if (serverIpStr.equals("")) {
			try {
				serverIpStr = Util.getLocalIpAddress(this);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return serverIpStr;
	}

	/**
	 * 保存用户输入的Ip
	 * 
	 * @param serverIpStr
	 *            用户输入Ip
	 */
	public void setServerIpStr(String serverIpStr) {
		this.serverIpStr = serverIpStr;
		saveIpStr(this.serverIpStr);
	}

	/**
	 * 从配置文件中获取存入的Ip
	 * 
	 * @return Ip地址
	 */
	private String readIpStr() {
		return mSharedPreferences.getString("IpStr", "");
	}

	/**
	 * 将Ip地址存入配置文件中
	 * 
	 * @param ipStr
	 *            Ip地址
	 */
	private void saveIpStr(String ipStr) {
		Editor editor = mSharedPreferences.edit();
		editor.putString("IpStr", ipStr);
		editor.commit();
	}

	/**
	 * 获得Handler对象
	 * 
	 * @return Handler对象
	 */
	public Handler getHandler() {
		return mHandler;
	}

	/**
	 * 显示对话框
	 */
	private void showAlertDialog(Activity activity) {
		AlertDialog.Builder builder = new AlertDialog.Builder(activity);
		builder.setTitle(getString(R.string.prompt));
		builder.setMessage(getString(R.string.no_net_work));
		builder.setPositiveButton(activity.getString(android.R.string.ok), null);
		mDialog = builder.create();
		mDialog.show();
	}

	/**
	 * 给Client设置Activity
	 * 
	 * @param activity
	 *            Activity
	 */
	public void setActivity(Activity activity) {
		showAlertDialog(activity);
	}

	/**
	 * 读取所有传感器预警值的范围
	 * 
	 */
	private void readSensorConfig() {
		mSensorConfig.minAirTemperature = mSharedPreferences.getInt(
				"minAirTemperature", mSensorConfig.minAirTemperature);// 空气温度最小值
		mSensorConfig.maxAirTemperature = mSharedPreferences.getInt(
				"maxAirTemperature", mSensorConfig.maxAirTemperature);// 空气温度最大值
		mSensorConfig.minAirHumidity = mSharedPreferences.getInt(
				"minAirHumidity", mSensorConfig.minAirHumidity);// 空气湿度最小值
		mSensorConfig.maxAirHumidity = mSharedPreferences.getInt(
				"maxAirHumidity", mSensorConfig.maxAirHumidity);// 空气湿度最大值
		mSensorConfig.minSoilTemperature = mSharedPreferences.getInt(
				"minSoilTemperature", mSensorConfig.minSoilTemperature);// 土壤温度最小值
		mSensorConfig.maxSoilTemperature = mSharedPreferences.getInt(
				"maxSoilTemperature", mSensorConfig.maxSoilTemperature);// 土壤温度最大值
		mSensorConfig.minSoilHumidity = mSharedPreferences.getInt(
				"minSoilHumidity", mSensorConfig.minSoilHumidity);// 土壤湿度最小值
		mSensorConfig.maxSoilHumidity = mSharedPreferences.getInt(
				"maxSoilHumidity", mSensorConfig.maxSoilHumidity);// 土壤湿度最大值

		mSensorConfig.minLight = mSharedPreferences.getInt("minLight",
				mSensorConfig.minLight);// 灯光强度最小值
		mSensorConfig.maxLight = mSharedPreferences.getInt("maxLight",
				mSensorConfig.maxLight);// 灯光强度最大值
		mSensorConfig.minCo2 = mSharedPreferences.getInt("minCo2",
				mSensorConfig.minCo2);// CO2浓度最小值
		mSensorConfig.maxCo2 = mSharedPreferences.getInt("maxCo2",
				mSensorConfig.maxCo2);// CO2浓度最大值
		mSensorConfig.controlAuto = mSharedPreferences.getInt("controlAuto",
				mSensorConfig.controlAuto);// 0表示手动控制，1表示自动控制
	}

	/**
	 * 保存所有传感器预警值的范围
	 * 
	 */
	public void saveSensorConfig() {
		Editor editor = mSharedPreferences.edit();
		editor.putInt("minAirTemperature", mSensorConfig.minAirTemperature);// 空气温度最小值
		editor.putInt("maxAirTemperature", mSensorConfig.maxAirTemperature);// 空气温度最大值
		editor.putInt("minAirHumidity", mSensorConfig.minAirHumidity);// 空气湿度最小值
		editor.putInt("maxAirHumidity", mSensorConfig.maxAirHumidity);// 空气湿度最大值
		editor.putInt("minSoilTemperature", mSensorConfig.minSoilTemperature);// 土壤温度最小值
		editor.putInt("maxSoilTemperature", mSensorConfig.maxSoilTemperature);// 土壤温度最大值
		editor.putInt("minSoilHumidity", mSensorConfig.minSoilHumidity);// 土壤湿度最小值
		editor.putInt("maxSoilHumidity", mSensorConfig.maxSoilHumidity);// 土壤湿度最大值

		editor.putInt("minLight", mSensorConfig.minLight);// 灯光强度最小值
		editor.putInt("maxLight", mSensorConfig.maxLight);// 灯光强度最大值
		editor.putInt("minCo2", mSensorConfig.minCo2);// CO2浓度最小值
		editor.putInt("maxCo2", mSensorConfig.maxCo2);// CO2浓度最大值
		editor.putInt("controlAuto", mSensorConfig.controlAuto);// 0表示手动控制，1表示自动控制
		editor.commit();
	}

	/**
	 * 获取各传感器的阀值
	 * 
	 * @return SensorConfig
	 */
	public SensorConfig getSensorConfig() {
		return mSensorConfig;
	}

	/**
	 * 获取当前各传感器的值
	 * 
	 * @return SensorValue
	 */
	public SensorValue getCurSensorValue() {
		return mCurSensorValue;
	}

}
